package twitterSearch; 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

public class SearchTwitter {
  Twitter twitter;
  int rpp = 100;
  public final int NUM_PAGES = 5;

  public SearchTwitter(final Twitter twitter) {
      this.twitter = twitter;
  } 

  /*public java.util.List<Tweet> listSearchResults(QueryResult q){
        java.util.List<Tweet> tweets = null;
        tweets = q.getTweets();                    
        return tweets;
  }*/
  
    public java.util.LinkedList<Tweet> getListSearchResults(String s, String lang) throws TwitterException{
        java.util.LinkedList<Tweet> tweets = new LinkedList<Tweet>();
        java.util.List<Tweet> auxTweets = null;
        QueryResult q;
        Boolean sair = false;

        for (int j = 1; j <= this.NUM_PAGES && !sair; j++){        
            q = twitter.search(new Query(s).lang(lang).page(j).rpp(rpp));
            auxTweets = q.getTweets();       
            if (auxTweets.size() < 100 )
                sair = true;
            for (int i = 0; i < auxTweets.size(); i++)
                tweets.add(auxTweets.get(i));
        }                   
        return tweets;
    }

  /*public String[] arraySearchResults(QueryResult q){
        java.util.List<Tweet> tweets = null;
        tweets = q.getTweets();

        String[] tweetArr = new String[tweets.size()];

        for (int i = 0; i < tweets.size(); i++) {
            tweetArr[i] = tweets.get(i).getFromUser() + ":  "+ tweets.get(i).getText();
        }            
        return tweetArr;
  }*/
  
    public String[] getArraySearchResults(String s, String lang) throws TwitterException{        
        java.util.List<Tweet> tweets = null;
        String[] tweetArr = new String[NUM_PAGES * rpp];
        QueryResult q;
        boolean sair = false;

        for (int j = 1; j <= this.NUM_PAGES && !sair; j++){        
            q = twitter.search(new Query(s).lang(lang).page(j).rpp(rpp));
            tweets = q.getTweets();       
            if (tweets.size() < 100 )
                sair = true;
            for (int i = 0; i < tweets.size(); i++) {
                tweetArr[i+(j-1)*this.rpp] = tweets.get(i).getFromUser() 
                        + ":  "+ tweets.get(i).getText();
            } 
        }
            return tweetArr;        
    }

  public QueryResult getQueryResult(String s, String lang) throws TwitterException{
      return twitter.search(new Query(s).rpp(rpp).lang(lang));
  }

  public void printTweets(String[] tweetArray){
      for(int i = 0; i<tweetArray.length; i++){
          System.out.println(i+ ") "+tweetArray[i]);
      }
  }
  
  public static Twitter getTwitter() throws IOException, TwitterException{
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer("jaBct5meAX3L676Tq00HA", "rA7FhUkPrAblbfrjy01sKuIJJKYYh9AGx9pthzd4s");
        RequestToken requestToken = twitter.getOAuthRequestToken();
        AccessToken accessToken = null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        // Ask user to enter autorization code
        while (null == accessToken) {
          System.out.println("Open this URL and grant access to your account:");
          System.out.println(requestToken.getAuthorizationURL());
          System.out.print("Enter the PIN(if available) or just hit enter:");
          String pin = br.readLine();
          try{
             if(pin.length() > 0){
               accessToken = twitter.getOAuthAccessToken(requestToken, pin);
             }else{
               accessToken = twitter.getOAuthAccessToken();
             }
          } catch (TwitterException te) {
            if(401 == te.getStatusCode()){
              System.out.println("Unable to get the access token.");
            }else{
              te.printStackTrace();
            }
          }
        }
        return twitter;
  }
}